package net.sk.china.city.talk.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import net.sk.china.city.talk.dao.system.SystemGroupDao;
import net.sk.china.city.talk.model.system.SystemGroup;
import net.sk.china.city.talk.request.system.SystemGroupRequest;
import net.sk.china.city.talk.request.system.SystemGroupSearchRequest;
import net.sk.china.city.talk.service.system.SystemGroupService;
import net.sk.china.city.talk.service.user.UserService;
import net.sk.china.common.request.PageParamRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * SystemGroupServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/3 14:35
 * // +----------------------------------------------------------------------
 */

@Service
public class SystemGroupServiceImpl extends ServiceImpl<SystemGroupDao, SystemGroup> implements SystemGroupService {

    @Resource
    private SystemGroupDao dao;

    private final UserService userService;

    public SystemGroupServiceImpl(UserService userService) {
        this.userService = userService;
    }


    /**
     * 分页显示组合数据表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     * @return {@code List<SystemGroup>}
     */
    @Override
    public List<SystemGroup> getList(SystemGroupSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 SystemGroup 类的多条件查询
        LambdaQueryWrapper<SystemGroup> query = new LambdaQueryWrapper<>();
        if(!StringUtils.isBlank(request.getKeywords())){
            query.like(SystemGroup::getName, request.getKeywords());
        }
        query.orderByDesc(SystemGroup::getId);
        return dao.selectList(query);
    }

    /**
     * 新增组合数据
     * @param systemGroupRequest 新增参数
     * @return {@code boolean}
     */
    @Override
    public boolean add(SystemGroupRequest systemGroupRequest) {
        SystemGroup systemGroup = new SystemGroup();
        BeanUtils.copyProperties(systemGroupRequest, systemGroup);
        return save(systemGroup);
    }

    /**
     * 删除组合数据表
     * @param id Integer
     * @return {@code boolean}
     */
    @Override
    public boolean delete(Integer id) {
        boolean remove = removeById(id);
        if (remove) {
            // 删除用户对应已经存在的分组标签 虽然数据库用的是String类型但逻辑仅仅只存储一个数据，这里直接删除对应的用户分组id即可
            userService.clearGroupByGroupId(id.toString());
        }
        return remove;
    }

    /**
     * 修改组合数据表
     * @param id integer id
     * @param systemGroupRequest 修改参数
     * @return {@code boolean}
     */
    @Override
    public boolean edit(Integer id, SystemGroupRequest systemGroupRequest) {
        SystemGroup systemGroup = new SystemGroup();
        BeanUtils.copyProperties(systemGroupRequest, systemGroup);
        systemGroup.setId(id);
        return updateById(systemGroup);
    }
}
